package com.ht.modules.system.util;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.ht.utils.HttpClient.doGet;

/**
 * 获取法定节假日工具类
 * @author ht
 */
@Component
public class HolidayUtil {

    /**
     * @param httpArg :参数
     * @return 返回结果
     */
    public static String request(String httpArg) {
        String httpUrl = "https://tool.bitefu.net/jiari/";
        BufferedReader reader;
        String result = null;
        StringBuilder sbf = new StringBuilder();
        httpUrl = httpUrl + "?d=" + httpArg;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            String strRead;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);

            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String request = request("20210614");
        System.out.println("request = " + request);
        String url = "https://tool.bitefu.net/jiari/?info=1&d=";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String yearMonth = sdf.format(new Date());
        String result = doGet(url + yearMonth);
        System.out.println("result = " + result);
    }

    /**
     * 根据格式化后的日期字符串获取是否为节假日
     * @param dateFormatString 格式化后的日期字符串，例：20210831
     * @return 是否为节假日标识
     */
    @Cacheable(value = "isHoliday", key = "#dateFormatString")
    public String getHolidayByDate(String dateFormatString){
        try{
            // workday 1-工作日，2-非工作日
            // holiday_legal 1-法定节假日，2-非法定节假日
            String url = "https://api.apihubs.cn/holiday/get?field=holiday_legal,workday&date="
                    + dateFormatString + "&cn=1&size=1";
            String result = doGet(url);
            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONObject data = jsonObject.getJSONObject("data");
            ArrayList<?> list = data.getObject("list", ArrayList.class);
            if (list != null && list.size() > 0) {
                Map<String, Object> map = (Map<String, Object>) list.get(0);
                return String.valueOf(map.getOrDefault("holiday_legal", "0"));
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return "-1";
    }

}
